package com.web.manager;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.web.domain.ApiResponse;
import com.web.utils.HttpClientBase;

import java.util.HashMap;
import java.util.Map;

/**
 * @author FDjavaone
 * @date 2017/8/31
 */
public class GaoDeManager {


    public static ApiResponse transferAddress(String address) {
        ApiResponse ponse = new ApiResponse();
        String host = "http://restapi.amap.com";
        String path = "/v3/geocode/geo";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("key", "d310aede8d893887515aca4ed27856d5");
        querys.put("address", address);

        try {
            HttpClientBase base = new HttpClientBase();
            String resultBody = base.doPost(host + path, querys, "utf-8");


            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;
            rootNode = objectMapper.readValue(resultBody, JsonNode.class);
            String resultCode = rootNode.get("status").asText();
            // 成功
            if ("1".equals(resultCode)) {
                JsonNode resultNode = rootNode.get("geocodes").get(0);
                String loc = resultNode.get("location").textValue();
                ponse.setStatus(900000);
                ponse.setMessage("获取成功");
                ponse.setParams(loc);
            } else {
                ponse.setStatus(900507);
                ponse.setMessage("地址转换坐标失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }

    public static ApiResponse transferLoc(String loc) {
        ApiResponse ponse = new ApiResponse();
        String host = "http://restapi.amap.com";
        String path = "/v3/geocode/regeo";
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("key", "d310aede8d893887515aca4ed27856d5");
        querys.put("location", loc);
        querys.put("extensions", "base");
        querys.put("batch", "false");
        querys.put("roadlevel", "1");
        try {
            HttpClientBase base = new HttpClientBase();
            String resultBody = base.doPost(host + path, querys, "utf-8");
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode rootNode;
            rootNode = objectMapper.readValue(resultBody, JsonNode.class);
            String resultCode = rootNode.get("status").asText();
            // 成功
            if ("1".equals(resultCode)) {
                JsonNode resultNode = rootNode.get("regeocode");
                ponse.setStatus(900000);
                ponse.setMessage("获取成功");
                ponse.setParams(resultNode.get("addressComponent"));
            } else {
                ponse.setStatus(900507);
                ponse.setMessage("地址转换坐标失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ponse;
    }
}
